<script type="text/javascript">

function LoadTP(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp == null)
{
alert ("Browser does not support HTTP Request");
return;
}

var url = "modules/customers/load_thanhpho.php";
url = url + "?sltinhthanh=" + str;

xmlhttp.onreadystatechange = stateChanged1;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}

function stateChanged1()
{
if (xmlhttp.readyState == 4)
{
document.getElementById("tinhthanh").innerHTML = xmlhttp.responseText;

}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}

</script>   
<table class="box-vang5" cellpadding="0" cellspacing="0" width="100%">
        <tbody><tr class="light">
          <td class="text-top">.:. Danh sách khách hàng</td>
        </tr>
      </tbody></table>
      <table class="box-vang5" cellpadding="0" cellspacing="0" width="100%">
        <tbody><tr class="light"> 
          <td class="t-l"></td>
          <td class="t-c"></td>
          <td class="t-r"></td>
        </tr>
        <tr class="light">
          <td class="c-l"></td>
          <td class="c-c" valign="top">
  <form action="" method="post">

     <p>
    &nbsp;&nbsp;
    <span>Tên khách hàng</span> 
    <span> 
    	<input type="text" style="width:150px;" name="TenKh"/>
    </span> 
    &nbsp;
    <span>Loại </span>
    <span> 
    	<select name="loaiKh"> 
        	<option value="">--Lựa chọn--</option>
            <option value="1">Cá nhân</option>
            <option value="2">Tổ chức</option>
        </select>
    </span>
    &nbsp;Người sở hữu<span> 
    	<select name="DonViQl"> 
        	<option value="">--Lựa chọn--</option>
        <?php $sql_dv=mysql_query("select room_id,room_name from tb_room");
			while($rs_dv=mysql_fetch_array($sql_dv))
			{
		?>    
            <option value="<?php echo $rs_dv['room_id'];?>"><?php echo $rs_dv['room_name'];?></option>
        <?php }?>   
        </select>
    </span>
    
     <span>Khu vực</span>
    <span> 
    <select name="sltinhthanh" style="width:100px" onchange="LoadTP(this.value)">                 
                        <option value="" selected="selected">--Lựa chọn--</option>
                         <?php 
                            $sqlstring="select tinhthanh_id,tinhthanh_tieude,tinhthanh_parent  from tbtinhthanh where tinhthanh_parent = 0 order by tinhthanh_id asc";									
                            $qr12=mysql_query($sqlstring);	
                            while($row=mysql_fetch_array($qr12)) 
                            {			
							
                            ?>               
                         <option  value="<?=$row['tinhthanh_id']?>"><?=$row['tinhthanh_tieude']?></option>
                          <?php }?>            
                            </select></span>
                            
                          <span id="tinhthanh">
                       
                    			<select  id="nv_giao" style="width:150px" >
                                <option value="0">--Lựa chọn--</option>
								</select>
                     </span>
  				
    
    
    <span><input name="button" type="submit" value="Tìm kiếm" class="cs-title-timkiem"/></span>
    &nbsp;&nbsp;</p>
    
 </form>
    <div class="clear" style="height:10px"></div>
<?php 
	$search=$_POST['TenKh'];
	$loaiKh=$_POST['loaiKh'];
	$DonViQl=$_POST['DonViQl'];
	$sltinhthanh = $_POST['sltinhthanh'];
	$idquanhuyen = $_POST['idquanhuyen'];
?>

<?php 
	$user=$_SESSION['userid'];
	$sqlstring="select  
						customers.id,
						customers.name_kh,
						customers.name_com,
						customers.telephone,
						customers.email,
						customers.update_time,
						customers.manager,
						customers.object,
						customers.manager_unit,
						customers.manager_other,
						customers.creater,						
						customers.care
					from 
						customers 
					where customers.creater='$user' or customers.manager='$user'
					";

	if($search!="" and $loaiKh=="" and $DonViQl==""){$sqlstring.=" and name_kh like '%$search%' or name_com like '%$search%'";}
	if($loaiKh!="" and $search=="" and $DonViQl=="")
		{
			$sqlstring.="and object='$loaiKh'";
		}
		if($DonViQl!="" and $search=="" and $loaiKh=="")
		{
			$sqlstring.="and manager_unit='$DonViQl'";
		}
		if($search!="" and $loaiKh!="" and $DonViQl!="")
		{
			$sqlstring.="and (name_kh like '%$search%' or name_com like '%$search%') and object='$loaiKh' and manager_unit='$DonViQl'";
		}
		if($search!="" and $loaiKh!="" and $DonViQl=="")
		{
			$sqlstring.="and (name_kh like '%$search%' or name_com like '%$search%') and object='$loaiKh'";
		}
		if($search=="" and $loaiKh!="" and $DonViQl!="")
		{
			$sqlstring.="and object='$loaiKh' and manager_unit='$DonViQl'";
		}
		if($search!="" and $loaiKh=="" and $DonViQl!="")
		{
			$sqlstring.="and (name_kh like '%$search%' or name_com like '%$search%') and manager_unit='$DonViQl'";
		}
		
	
	$qr1=mysql_query($sqlstring); 
	$i=0;
	
  ?>
<table id="box-quick" class="box-quick" border="0" cellpadding="5" cellspacing="0" width="100%">
          <tbody><tr id="title">
            <td width="5%">STT</td>
            <td width="20%">Tên khách hàng</td>
             <td width="10%">Điện thoại</td>
            <td width="10%">Khu vực</td>
            <td width="10%">Người Sở hữu</td>
            <td width="15%">Ngày cập nhật</td>
            <td width="5%">Còn</td>
            <td width="6%">Thao tác</td>
          </tr>
         <?php 
		 	while($row=mysql_fetch_array($qr1)) 
			{
				$i=$i+1;
				$ids=$row['id'];	
				$care = $row['care'];						
				$tinhthanh_str=$row['tinhthanh_str'];
				$tinhthanh=explode("~",$tinhthanh_str);
				$idtinhthanh=str_replace("T","",$tinhthanh[0]);
				$idquanhuyen=str_replace("Q","",$tinhthanh[1]);
				$tinhthanh=catchinfor($idtinhthanh,"tinhthanh_id","tinhthanh_tieude","tbtinhthanh");	
				
		 ?>
           <tr class="light" id="value" style="cursor:pointer;" onmouseover="mouse_den(this)" onmouseout="mouse_di(this)" bgcolor="">
            <td style="border-left:1px solid #ccc"><div align="center"><?php echo $i;?></div></td>
            <td  class="tdbg_2"><div align="left">&nbsp;<span title="">
				<a href="index.php?tn=customers&kh=detail&id=<?php echo $row['id'];?>"><?php if($row['object']==1){echo $row['name_kh'];}else{echo $row['name_com'];}?></a>
                 </span>
                </div>
            </td>
           <td align="center"> <?php echo $row['telephone']?> - <? echo $sltinhthanh;?> - <? echo $idquanhuyen;?></td>
             <td><div align="center">&nbsp;<?php  echo $tinhthanh?></div></td>
            <td>
            	<div align="center" style="color:#090;">
					<?php 
						$sql_ql=mysql_query("select * from tb_account_admin where account_id='".$row['creater']."'");
						while($rs_ql=mysql_fetch_array($sql_ql))
						{
							echo $rs_ql['account_name'];
						}
					?>
            	</div>
            </td>
            <td align="center"> <?php echo date('d/m/Y h:i a',$row["update_time"]);?> </td>
           <td align="center"> <?php echo $row["care"];?> </td>
             <td align="center">          	
				<a onmouseout="menuLayers.hide();" onclick="menuLayers.show('<?php echo $row['id'];?>_action_kh', event); "><img width="16" align="absmiddle" src="../skins/images/Thao_Tac_list.png"></a>
													
				<a onmouseout="menuLayers.hide();" onclick="menuLayers.show('<?php echo $row['id'];?>_action_kh_view', event); "><img width="16" align="absmiddle" src="../skins/images/find.png"></a>
													
                &nbsp;
                            
             </td>
          </tr>
        <div id="<?php echo $row['id'];?>_action_kh" class="layermask" style="left: 1008px; top: 112px; visibility: hidden;">
	<ul>
        <li onclick="window.location='index.php?tn=customers&kh=edit&id=<?php echo $row['id'];?>';" style="cursor:pointer;"><img src="../skins/images/edit.png" style="float: left"> <p>Sửa</p>
        </li>
        <li onclick="if(confirm('Bạn muốn xóa khách hàng này không?')){window.location='index.php?tn=customers&kh=del&id=<?php echo $row['id'];?>';}" style="cursor:pointer;">
        <img src="../skins/images/del.png" style="float: left"> <p>Xóa</p>
        </li>
        <? if ($care<=0){?>
          <li onclick="window.location='index.php?tn=customers&kh=get&id=<?php echo $row['id'];?>';" style="cursor:pointer;"><img src="../skins/images/email.png" style="float: left"> <p>Tiếp nhận khách này</p>
        </li>
        <? }?>
         <li onclick="window.location='index.php?tn=customers&kh=Dmll_add';" style="cursor:pointer;"><img src="../skins/images/item4.png" style="float: left"> <p>Thêm đầu mối liên lạc</p></li>
         <li onclick="window.location='index.php?tn=customers&kh=cohoi_add';" style="cursor:pointer;"><img src="../skins/images/coins.png" style="float: left"> <p>Thêm cơ hội bán hàng</p>
        </li>
         <li onclick="window.location='';" style="cursor:pointer;"><img src="../skins/images/contract.png" style="float: left"> <p>Thêm hợp đồng</p>
        </li>
        <li onclick="window.location='index.php?tn=congviec&cv=phatsinh';" style="cursor:pointer;"><img src="../skins/images/task.gif" style="float: left"> <p>Thêm công việc</p>
        </li>
       
	</ul>				
</div>  
<div id="<?php echo $row['id'];?>_action_kh_view" class="layermask" style="left: 1053px; top: 190px; visibility: hidden;">													
<ul>
		<?php $sql_dmll= mysql_query("select count(*) as dmll from daumoilienlac where khachhang_id='".$row['id']."'");
			$row_dmll=mysql_fetch_array($sql_dmll);
		?>
		<li onclick="javascript:window.location='index.php?tn=customers&kh=Dmll_kh&kh_id=<?php echo $row['id'];?>'" style="cursor:pointer;"><img src="../skins/images/danh-sach.gif" style="float: left"> <p>Đầu mối liên lạc&nbsp; (<?php echo $row_dmll['dmll'];?>)</p>
	</li>
		<li onclick="javascript:window.location='http://tamnguyen.gencrm.com/modules/customers/edit?&amp;view=1&amp;p=3&amp;tab=4'" style="cursor:pointer;"><img src="../skins/images/danh-sach.gif" style="float: left"> <p>Cơ hội bán hàng&nbsp; (0)</p>
	</li>
	<li onclick="javascript:window.location='http://tamnguyen.gencrm.com/modules/customers/edit?&amp;view=1&amp;p=3&amp;tab=5'" style="cursor:pointer;"><img src="../skins/images/danh-sach.gif" style="float: left"> <p>Hợp đồng&nbsp; (0)</p>
	</li>
	<li onclick="javascript:window.location='http://tamnguyen.gencrm.com/modules/customers/edit?&amp;view=1&amp;p=3&amp;tab=6'" style="cursor:pointer;"><img src="../skins/images/danh-sach.gif" style="float: left"> <p>Công việc&nbsp; (0)</p>
</li></ul>												
</div>
         <?php }?>
         
          
        </tbody>
	</table>


      
        </td>
      
      
 <td class="c-r"></td>
        </tr>
        <tr class="light">
          <td class="b-l"></td>
          <td class="b-c"></td>
          <td class="b-r"></td>
        </tr>
      </tbody></table>